package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserService;
import com.tanhua.server.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Value("${tanhua.secret}")
    private String secret;
    @Autowired
    private UserService userService;

    /**
     * 手机号登录
     */
    @PostMapping("/login")
    public ResponseEntity sendVerificationCode(@RequestBody Map map) {
        String mobile = (String) map.get("phone");
        return userService.sendVerificationCode(mobile);
    }

    /**
     * 验证码校验
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map) {
        String mobile = (String) map.get("phone");
        String verificationCode = (String) map.get("verificationCode");
        return userService.loginVerification(mobile, verificationCode);
    }

    /**
     * 信息完善
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo) {

        User user = UserHolder.getUser();
        userInfo.setId(user.getId());
        return userService.saveUserInfo(userInfo);
    }

    /**
     * 保存头像
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity saveAvatar(MultipartFile headPhoto) throws IOException {
        User user = UserHolder.getUser();
        return userService.saveAvatar(headPhoto, user.getId());
    }
}
